package com.kinoshita.admin.controller.web;

import com.kinoshita.admin.api.domain.RoleDO;
import com.kinoshita.admin.api.query.RoleQueryDO;
import com.kinoshita.admin.service.IRoleService;
import com.kinoshita.common.result.PageDO;
import com.kinoshita.common.result.ResultDO;
import com.kinoshita.common.result.WebResultDO;
import com.kinoshita.common.status.Constant;
import com.kinoshita.common.status.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author reagan
 * @since 2022-08-29
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService RoleService;

    @RequestMapping(value="/add", method = RequestMethod.POST)
    public @ResponseBody WebResultDO add(@RequestBody RoleDO domain) {
        ResultDO resultDO = RoleService.add(domain);
        if(!resultDO.isSuccess()) {
            return new WebResultDO(resultDO.getStatusCode());
        }
        return new WebResultDO(StatusCode.SYSTEM_SUCCESS);
    }

    @RequestMapping(value="/edit", method = RequestMethod.POST)
    public @ResponseBody WebResultDO edit(@RequestBody RoleDO domain) {
        ResultDO resultDO = RoleService.edit(domain);
        if(!resultDO.isSuccess()) {
            return new WebResultDO(resultDO.getStatusCode());
        }
        return new WebResultDO(StatusCode.SYSTEM_SUCCESS);
    }

    @RequestMapping(value="/delete", method = RequestMethod.POST)
    public @ResponseBody WebResultDO delete(@RequestBody RoleDO domain) {
        ResultDO resultDO = RoleService.delete(domain.getId());
        if(!resultDO.isSuccess()) {
            return new WebResultDO(resultDO.getStatusCode());
        }
        return new WebResultDO(StatusCode.SYSTEM_SUCCESS);
    }

    @RequestMapping(value="/detail", method = RequestMethod.POST)
    public @ResponseBody WebResultDO<RoleDO> detail(@RequestBody RoleDO domain) {
        ResultDO resultDO = RoleService.get(domain.getId());
        if(!resultDO.isSuccess()) {
            return new WebResultDO(resultDO.getStatusCode());
        }
        return new WebResultDO(StatusCode.SYSTEM_SUCCESS, resultDO.getModel(Constant.DATA_MODEL_KEY));
    }

    @RequestMapping(value="/query", method = RequestMethod.POST)
    public @ResponseBody WebResultDO<PageDO<RoleDO>> query(@RequestBody RoleQueryDO query) {
        ResultDO resultDO = RoleService.query(query);
        if(!resultDO.isSuccess()) {
            return new WebResultDO(resultDO.getStatusCode());
        }
        return new WebResultDO(StatusCode.SYSTEM_SUCCESS,  resultDO.getModel(Constant.DATA_MODEL_KEY));
    }
}
